package piece1.chapter5;

public class Problem5_11 {

    /**
     * @param args
     */
    public static void main(String[] args) {
	// TODO 自动生成的方法存根
	int a[] = new int[10];
	for (int i = 0; i < a.length; i++) {
	    a[i] = (int) (Math.random() * 1000);
	    System.out.print(a[i] + "\t");
	}
	System.out.println();
	System.out.println("均值为" + mean(a));
	System.out.println("标准差为" + String.format("%.04f", deviation((a))));
    }

    public static double mean(int a[]) {
	double sum = 0, mean;
	for (int i = 0; i < a.length; i++) {
	    sum += a[i];
	}
	mean = sum / (a.length);
	return mean;

    }

    public static double deviation(int a[]) {
	double sumOfSquares = 0;
	double meanOfThesquaresOfSum;
	double deviation, variance;
	for (int i = 0; i < a.length; i++) {

	    sumOfSquares += Math.pow(a[i], 2);
	}
	meanOfThesquaresOfSum = (Math.pow(mean(a), 2)) / a.length;
	variance = (sumOfSquares - meanOfThesquaresOfSum) / (a.length - 1);
	deviation = Math.sqrt(variance);
	return deviation;
    }
}